package com.bluesky.light.chart;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;

import com.bluesky.light.order.OrderService;

/**
 * @author Lemon
 *
 */
public class FinanicalChart {
	
	/**
	 * @param session
	 * @return
	 */
	public static String getFilename(HttpSession session) {
		String filename = "";
		/*
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		dataset.addValue(610, "china", "port");
		dataset.addValue(320, "china", "beef");
		dataset.addValue(530, "china", "chickren");
		dataset.addValue(340, "china", "fish");
		JFreeChart chart = ChartFactory.createBarChart3D("Meat", 
		                  "Meat",
		                  "Sold",
		                  dataset,
		                  PlotOrientation.VERTICAL,
		                  false,
		                  false,
		                  false);		
		try {
			filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session);
		} catch (IOException e) {
			e.printStackTrace();
		}
		*/
		Date date = new Date();
		List<Double> finanicalDates = OrderService.getInstance().getFinanicalDates(date);
		
		double[][] data = new double[finanicalDates.size()][1];
		String[] rowKeys = new String[finanicalDates.size()];
		String[] colKeys = {""};		
		for (int i = 0; i < finanicalDates.size(); i++) {
			data[i][0] = finanicalDates.get(i);
			rowKeys[i] = Integer.toString(i+1);
		}
		
		/*
		double[][] data = new double[][] {{1310}, {720}, {1130}, {440}};
		String[] rowKeys = {"猪肉", "牛肉","鸡肉", "鱼肉"};
		String[] colKeys = {""};
		*/
		CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, colKeys, data);
		JFreeChart chart = ChartFactory.createBarChart3D("Income", "", "Amount", dataset, PlotOrientation.VERTICAL, true, false, false);
		
		try {
			filename = ServletUtilities.saveChartAsPNG(chart, 700, 300, null, session);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return filename;
	}
}
